/*! ******************************************************************************
 *
 * Created on 2021年11月29日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.pentaho.di.ui.spoon.navigation.view.subtree;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.TreeItem;
import org.pentaho.di.core.plugins.PluginInterface;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.plugins.StepPluginType;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.spoon.navigation.view.Subtree;
import org.pentaho.di.ui.spoon.navigation.view.MetaTree;

/**
 * 步骤子树
 * 
 * @author hs@yzbdl.ac.cn
 *
 */
public class StepsSubtree extends Subtree {

	/**
	 * @param parent
	 */
	public StepsSubtree(MetaTree parent, TransMeta meta) {
		super(parent);
		// Put the steps below it.
		for (int i = 0; i < meta.nrSteps(); i++) {
			StepMeta stepMeta = meta.getStep(i);
			if (stepMeta.isMissing()) {
				continue;
			}
			PluginInterface stepPlugin = PluginRegistry.getInstance().getPlugin(StepPluginType.class,
					stepMeta.getStepID(), stepMeta.getVersion());

			if (!this.getSpoonRef().filterMatch(stepMeta.getName())) {
				continue;
			}
			Image stepIcon=null;
			if(stepPlugin!=null) {
				 stepIcon = GUIResource.getInstance().getImagesStepsSmall().get(stepPlugin.getId());
			}
			if (stepIcon == null) {
				stepIcon = GUIResource.getInstance().getImageFolder();
			}

			TreeItem tiStep = new TreeItem(this.getView(), SWT.NONE);
			tiStep.setText(stepMeta.getName());
			tiStep.setImage(stepIcon);

			if (stepMeta.isShared()) {
				tiStep.setFont(GUIResource.getInstance().getFontBold());
			}
			if (!stepMeta.isDrawn()) {
				tiStep.setForeground(GUIResource.getInstance().getColorDarkGray());
			}
		}
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.pentaho.di.ui.spoon.navigation.view.Subtree#getName()
	 */
	@Override
	public String getName() {
		return Spoon.STRING_STEPS;
	}

}
